package com.jdbc;

import com.pojo.Stu;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

/**
 * jdbc ResultSet 详解
 */
public class Jdbc2_ResultSet {
    public static void main(String[] args) throws Exception {
        // 1 . 注册驱动
        //Class.forName("com.mysql.jdbc.Driver");


        // 2. 获取连接
        String url = "jdbc:mysql://127.0.0.1:3306/bd1?useSSL=false";
        String username = "root";
        String password = "1234";
        Connection connection = DriverManager.getConnection(url, username, password);

        //sql
        String sql = "select * from stu";

        //获取statement
        Statement statement = connection.createStatement();

        //执行sql
        ResultSet resultSet = statement.executeQuery(sql);

        ArrayList<Stu> stuArrayList = new ArrayList<>();

        //处理结果
        while(resultSet.next()){
            Stu stu = new Stu();

            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            int age = resultSet.getInt("age");
            String sex = resultSet.getString("sex");
            String address = resultSet.getString("address");
            int math = resultSet.getInt("math");
            int english = resultSet.getInt("english");
            String date = resultSet.getString("hire_date");

            stu.setId(id);
            stu.setName(name);
            stu.setAge(age);
            stu.setSex(sex);
            stu.setAddress(address);
            stu.setMath(math);
            stu.setEnglish(english);
            stu.setDate(date);

            stuArrayList.add(stu);
        }

        System.out.println(stuArrayList);
        //7 .释放资源
        resultSet.close();
        statement.close();
        connection.close();

    }
}
